/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.jdbc.connection;

import com.bionic.manager.Config;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author Obuhov D.G.
 */
public class ConnectionPool {

    private static ConnectionPool instance = null;
    private DataSource pool;
    private final String DATASOURCE;

    public ConnectionPool() {
        DATASOURCE = Config.getInstance().getProperty(Config.DATASOURCE);
        initialPool();
    }

    public static synchronized ConnectionPool getInstance() {
        if (instance == null) {
            instance = new ConnectionPool();
        }
        return instance;
    }

    private void initialPool() {
        try {
            Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:comp/env/");
            pool = (DataSource) envContext.lookup(DATASOURCE);
        } catch (NamingException e) {
            e.printStackTrace();
        }


    }

    public synchronized Connection getConnection() throws SQLException {
        return pool.getConnection();
    }

    public void closeConnection(Connection connection) {
        System.out.println("");
    }
}